package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.AdminTopicService;
import com.cskaoyan.service.AdminTopicServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: AdminTopicServlet
 * @Description: TODO
 * @Author: jenyutao@gmail.com
 * @date: 2023/5/31 10:30
 * @Version: V1.0
 */

@WebServlet("/admin/topic/*")
public class AdminTopicServlet extends WdBaseServlet {

    AdminTopicService adminTopicService = new AdminTopicServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {

        String page = request.getParameter("page");
        int page1 = Integer.parseInt(page);
        String limit = request.getParameter("limit");
        int limit1 = Integer.parseInt(limit);
        String title = request.getParameter("title");
        String subtitle = request.getParameter("subtitle");
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        BaseRespVo baseRespVo = adminTopicService.list(page1, limit1, sort, order, title, subtitle);

        return baseRespVo;
    }


    private BaseRespVo read(HttpServletRequest request, HttpServletResponse response) {

        String id = request.getParameter("id");

        BaseRespVo baseRespVo = adminTopicService.read(Integer.parseInt(id));

        return baseRespVo;
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {

        String s = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(s, MarketTopic.class);

        BaseRespVo ok;
        if (marketTopic != null) {
            ok = adminTopicService.create(marketTopic);
        } else {
            ok = BaseRespVo.topicNo();
        }

        return ok;
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {

        String s = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(s, MarketTopic.class);

        BaseRespVo baseRespVo = adminTopicService.update(marketTopic);

        return baseRespVo;

    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {

        String s = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(s, MarketTopic.class);

        BaseRespVo baseRespVo = adminTopicService.delete(marketTopic);

        return baseRespVo;

    }

    @SneakyThrows
    private BaseRespVo batchdelete(HttpServletRequest request, HttpServletResponse response){

        // ArrayList <MarketTopic> marketTopics = new ArrayList <>();


        Map read = JacksonUtil.read(request.getReader().readLine(), Map.class);



        BaseRespVo baseRespVo = adminTopicService.deleteAll(read);

        return baseRespVo;

    }


}
